function hfile = srf_ShrinkSpikes(hfile, factor)
% SRF::ShrinkSpikes  - shrink marked spikes
%
% FORMAT:       [srf] = srf.ShrinkSpikes([factor]);
%
% Input fields:
%
%       factor      shrinking factor (default: 1)
%
% Output fields:
%
%       srf         altered SRF

% Version:  v0.7b
% Build:    7090215
% Date:     Sep-02 2007, 3:34 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% check arguments
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'srf')
    error( ...
        'BVQXfile:BadArguments', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);
if nargin < 2 || ...
   ~isa(factor, 'double') || ...
    numel(factor) ~= 1 || ...
    isinf(factor) || ...
    isnan(factor) || ...
    factor <= 0
    factor = 1;
else
    factor = real(factor);
end
factpp = factor + 1;

% get neighbors and colors
nb = bc.Neighbors;
co = bc.VertexColor;
cm = co(:, 1);

% iterate over spike vertices
for vc = find(isnan(cm(:)'))
    
    % first check that at least one neighbor is also marked
    nl = nb{vc, 2};
    if ~any(isnan(cm(nl)))
        co(vc, 1) = 0;
        
    % if good then combine color
    else
        co(vc, 2:4) = (factor * co(vc, 2:4) + sum(co(nl, 2:4))) / factpp;
    end
end

% store back
bc.VertexColor = co;
bvqxfile_setcont(hfile.L, bc);
